package com.xkstream.client.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xkstream.client.R;
import com.xkstream.client.application.Application;
import com.xkstream.client.base.AnimationListenerAdapter;
import com.xkstream.client.base.BaseActivity;
import com.xkstream.client.base.BaseData;
import com.xkstream.client.entity.kt.RewardData;
import com.xkstream.client.presenter.kt.RewardContract;
import com.xkstream.client.presenter.kt.RewardPresenter;
import com.xkstream.client.ui.adapter.RewardAdapter;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindColor;
import butterknife.BindString;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by xiaoyi on 2017-9-27.
 * 分成奖励
 */

public class RewardActivity extends BaseActivity<RewardContract.Presenter>
        implements RewardContract.View {
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.tvReward)
    TextView tvReward;
    @BindView(R.id.tvCount)
    TextView tvCount;
    @BindView(R.id.recycler_reward)
    RecyclerView recyclerReward;
    @BindView(R.id.cover)
    View cover;
    @BindView(R.id.llFilter)
    LinearLayout llFilter;

    @BindView(R.id.tvWeekly)
    TextView tvWeekly;
    @BindView(R.id.tvMonthly)
    TextView tvMonthly;
    @BindView(R.id.tvRecentMonth)
    TextView tvRecentMonth;
    @BindView(R.id.tv3Month)
    TextView tv3Month;
    private LinearLayoutManager layoutManager;
    RewardAdapter adapter;
    List<RewardData.GiftNumberValue> mDatas = new ArrayList<>();

    @BindColor(R.color.text_title)
    int colorSelected;
    @BindColor(R.color.login_input_selected)
    int colorUnselected;

    @BindString(R.string.reward_monthly)
    String recentMonthly;

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_reward);
        ButterKnife.bind(this);
        setTitle(R.string.reward);
        tvRight.setText(R.string.filter);
        tvRight.setVisibility(View.VISIBLE);
        presenter = new RewardPresenter(this);
        adapter = new RewardAdapter(this, mDatas);
        layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerReward.setAdapter(adapter);
        recyclerReward.setLayoutManager(layoutManager);

        setFilterSelected(1);
    }


    @OnClick({R.id.headerBack, R.id.tvRight,
            R.id.tvWeekly, R.id.tvMonthly, R.id.tvRecentMonth, R.id.tv3Month})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.headerBack:
                finish();
                break;
            case R.id.tvRight:
                showFilter();
                break;
            case R.id.tvWeekly:
                setFilterSelected(0);
                break;
            case R.id.tvMonthly:
                setFilterSelected(1);
                break;
            case R.id.tvRecentMonth:
                setFilterSelected(2);
                break;
            case R.id.tv3Month:
                setFilterSelected(3);
                break;
        }
    }


    private void showFilter() {
        llFilter.setVisibility(View.VISIBLE);
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.pw_top_in);
        llFilter.setAnimation(anim);
        anim.setAnimationListener(new AnimationListenerAdapter() {
            @Override
            public void onAnimationStart(Animation animation) {
                super.onAnimationStart(animation);

                cover.setVisibility(View.VISIBLE);
            }
        });
        llFilter.startAnimation(anim);
        cover.setOnClickListener((v) -> {
            hideFilter();

        });
    }


    public void hideFilter() {
        Animation anim = AnimationUtils.loadAnimation(this, R.anim.pw_top_out);
        llFilter.setAnimation(anim);
        anim.setAnimationListener(new AnimationListenerAdapter() {
            @Override
            public void onAnimationStart(Animation animation) {
                super.onAnimationStart(animation);
                llFilter.setVisibility(View.GONE);
                cover.setVisibility(View.GONE);
            }
        });
        llFilter.startAnimation(anim);
    }

    @Override
    public void onBackPressed() {
        if (cover.getVisibility() == View.VISIBLE) {
            hideFilter();
        } else {
            super.onBackPressed();
        }
    }

    private void setFilterSelected(int index) {
        tvWeekly.setTextSize(TypedValue.COMPLEX_UNIT_SP, index == 0 ? 17 : 14);
        tvMonthly.setTextSize(TypedValue.COMPLEX_UNIT_SP, index == 1 ? 17 : 14);
        tvRecentMonth.setTextSize(TypedValue.COMPLEX_UNIT_SP, index == 2 ? 17 : 14);
        tv3Month.setTextSize(TypedValue.COMPLEX_UNIT_SP, index == 3 ? 17 : 14);

        tvWeekly.setBackgroundResource(index == 0 ? R.drawable.money_selected : R.drawable.money_unselected);
        tvMonthly.setBackgroundResource(index == 1 ? R.drawable.money_selected : R.drawable.money_unselected);
        tvRecentMonth.setBackgroundResource(index == 2 ? R.drawable.money_selected : R.drawable.money_unselected);
        tv3Month.setBackgroundResource(index == 3 ? R.drawable.money_selected : R.drawable.money_unselected);

        tvWeekly.setTextColor(index == 0 ? colorSelected : colorUnselected);
        tvMonthly.setTextColor(index == 1 ? colorSelected : colorUnselected);
        tvRecentMonth.setTextColor(index == 2 ? colorSelected : colorUnselected);
        tv3Month.setTextColor(index == 3 ? colorSelected : colorUnselected);

        hideFilter();

        getRewardInfo(String.valueOf(index + 1));
    }


    @Override
    public void showRewardInfo(@NotNull BaseData<RewardData> data) {
        mDatas.clear();
        if (data.getData() != null) {
            mDatas.addAll(data.getData().getGift_number_value());
            adapter.customNotifyDataSetChanged();
//            tvReward.setText(String.format(recentMonthly, data.getData().getMonth().getMonth_value()));
            tvCount.setText(String.valueOf(data.getData().getMonth().getMonth_value()));
        }
    }

    @Override
    public void showError(@NotNull String message) {
        showTopTips(message);
    }

    @Override
    public void setPresenter(RewardContract.Presenter presenter) {
        this.presenter = presenter;
    }

    private void getRewardInfo(String type) {
        Map<String, Object> params = new HashMap<>();
        params.put("anchor_user_code", Application.getUser().getUser_code());
        params.put("currency", "CNY");
        params.put("type", type);
        presenter.getRewardInfo(params);
    }


    private void testMonth() {
        for (int i = 0; i < 10; i++) {
            RewardData.GiftNumberValue reward = new RewardData.GiftNumberValue();
            reward.setGift_time("2017-08-1 08:23:41" + i);
            reward.setAnchor_get_value((int) (44 + 0.5 * i * 4));
            mDatas.add(reward);
        }
        adapter.customNotifyDataSetChanged();
    }


}
